//
//  ViewController.swift
//  TableView
//
//  Created by Garvin on 2020/5/4.
//  Copyright © 2020 Garvin. All rights reserved.
//

import UIKit


class ViewController: UIViewController {
    
    var tableView: UITableView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
//        protocolDemo()
        createTableView()
    }
    
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {
    
    var cellID: String { return "CELLID" }
    
    func createTableView() {
        tableView = UITableView(frame: UIScreen.main.bounds, style: UITableView.Style.plain)
        tableView?.dataSource = self
        tableView?.delegate   = self
        tableView?.register(TableViewCell.self, forCellReuseIdentifier: cellID)
        self.view.addSubview(tableView!)
        print("cellID = \(cellID)")
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 50
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: cellID)
        cell.textLabel?.text = "cell \(indexPath.row)"
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50.0
    }
}

extension ViewController: SendResult {
    
    func sendMessage(msg: String) {
        print("msg:\(msg)")
    }
    
    func send(msg: String) -> String {
        return ""
    }
    
    func protocolDemo() {
        self.navigationController?.navigationBar.isTranslucent = false
        self.extendedLayoutIncludesOpaqueBars = true
        
        let clickButton = UIButton(type: UIButton.ButtonType.custom)
        clickButton.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
        clickButton.backgroundColor = UIColor.red
        clickButton.setTitle("点击去下一页", for: UIControl.State.normal)
        clickButton.addTarget(self, action: #selector(clickAction(_:)), for: UIControl.Event.touchUpInside)
        self.view.addSubview(clickButton)
    }
    
    @objc func clickAction(_ button: UIButton) {
        
        let secondVC = SecondViewController()
        secondVC.delegate = self
        self.navigationController?.pushViewController(secondVC, animated: true)
    }
}

